﻿using System;

namespace WJP.DataPortal
{
    /// <summary>
    /// 数据门户。
    /// 内部封装了对数据层的调用，如果是远程，则使用对应的代理来访问，这使得单机版、网络版的调用完全一致。
    /// </summary>
    public static class DataPortalApi
    {
        /// <summary>使用门户查询</summary>
        /// <param name="objectType"></param>
        /// <param name="criteria"></param>
        /// <param name="loc">如果一个数据层方法需要在本地执行，应该在把本参数指明为 Local。</param>
        /// <returns></returns>
        public static object Fetch(Type objectType, object criteria, DataPortalLocation loc = DataPortalLocation.Dynamic)
        {
            try
            {
                ++PlatformEnvironment.ThreadPortalCount;
                if (loc == DataPortalLocation.Local || !PlatformEnvironment.NeetProxy())
                    return FinalDataPortal.Fetch(objectType, criteria);
                return PortalApi.Fetch(objectType, criteria);
            }
            finally
            {
                --PlatformEnvironment.ThreadPortalCount;
            }
        }

        public static object Update(object obj, DataPortalLocation loc = DataPortalLocation.Dynamic)
        {
            try
            {
                ++PlatformEnvironment.ThreadPortalCount;
                if (loc != DataPortalLocation.Local && PlatformEnvironment.NeetProxy())
                    return PortalApi.Update(obj);
                FinalDataPortal.Update(obj);
                return obj;
            }
            finally
            {
                --PlatformEnvironment.ThreadPortalCount;
            }
        }
    }
}